# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=helix
pkgver=23.10
pkgrel=0
pkgdesc="A post-modern modal text editor"
url="https://helix-editor.com/"
arch="aarch64 armhf armv7 ppc64le x86 x86_64" # blocked by rust/cargo
license="MPL-2.0"
makedepends="cargo cargo-auditable"
checkdepends="tree-sitter-rust tree-sitter-ruby"
options="net"
install="$pkgname.post-install"
# This is needed just for tests.
_tsrust_ver="0.20.4"
source="https://github.com/helix-editor/helix/archive/$pkgver/helix-$pkgver.tar.gz
	https://github.com/tree-sitter/tree-sitter-rust/archive/v$_tsrust_ver/tree-sitter-rust-$_tsrust_ver.tar.gz
	runtime-dir-fhs-location.patch
	"

# Don't build tree-sitter languages, we provide them in standalone aports.
export HELIX_DISABLE_AUTO_GRAMMAR_BUILD=1

prepare() {
	default_prepare

	# This is needed for running tests.
	mkdir -p runtime/grammars/sources
	ln -s /usr/lib/tree-sitter/rust.so runtime/grammars/rust.so
	ln -s /usr/lib/tree-sitter/ruby.so runtime/grammars/ruby.so
	ln -s "$srcdir"/tree-sitter-rust-$_tsrust_ver runtime/grammars/sources/rust

	cargo fetch --target="$CTARGET" --locked
}

build() {
	cargo auditable build --frozen --release
}

check() {
	# This is needed for following tests:
	#	helix-vcs::src::git::test::modified_file
	#	helix-vcs::src::git::test::symlink
	#	helix-vcs::src::git::test::unmodified_file
	unset GIT_CEILING_DIRECTORIES

	# XXX: test_treesitter_indent_cpp is somehow broken
	cargo test --frozen --workspace -- \
		--skip test_treesitter_indent_cpp
}

package() {
	install -D -m755 target/release/hx -t "$pkgdir"/usr/bin/

	install -D -m644 contrib/Helix.desktop -t "$pkgdir"/usr/share/applications/
	install -D -m644 logo.svg "$pkgdir"/usr/share/icons/hicolor/scalable/apps/Helix.svg

	install -d -m755 "$pkgdir"/usr/share/$pkgname
	cp -r runtime "$pkgdir"/usr/share/$pkgname/

	rm -rf "$pkgdir"/usr/share/$pkgname/runtime/grammars
	ln -Ts ../../../lib/tree-sitter "$pkgdir"/usr/share/$pkgname/runtime/grammars
}

sha512sums="
6802dd878d2e4ea0c50694e85947ffd0849f7cd5b51d311c99f9a30d389680ffda77b8f0d178256786cda41f2e6122a1d6e7730734844b8a754748e3274c4dd9  helix-23.10.tar.gz
f1b2e9537286921056eda4e96faa9f61131b9ca5684e7ab8aa7bd07e46d91e5eab18e6e292cfad7cdcb7ba195e80f7f1e35f04992cb8011bbd29dd2d95116f9d  tree-sitter-rust-0.20.4.tar.gz
0d72f5528106df006764a201caf7fefa860e67646198818fd744257050ac566082569b289f0a1815dd14f0b1364e344f8841e3c6757377d8452d8ebd5b022d29  runtime-dir-fhs-location.patch
"
